package com.paddyi.shiro;

import com.paddyi.model.Resource;
import com.paddyi.model.Role;
import com.paddyi.model.vo.RoleVo;
import com.paddyi.model.vo.UserVo;
import com.paddyi.service.IRoleService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.filter.AccessControlFilter;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: paddyi
 * @description: MyAccessControlFilter
 * @author: paddy
 * @create: 2019-06-13 15:32
 **/
public class MyAccessControlFilter extends AccessControlFilter {

    @Autowired
    private IRoleService roleService;

    @Override
    protected boolean isAccessAllowed(ServletRequest servletRequest, ServletResponse servletResponse, Object o) throws Exception {
        HttpServletRequest request =(HttpServletRequest) servletRequest;
        HttpServletResponse response =(HttpServletResponse) servletResponse;
        if (request.getMethod().equals(RequestMethod.OPTIONS.name())) {
            return true;
        }
        //请求的url
        String requestURL = getPathWithinApplication(request);
        if (antMatchers(this.toList("/ajaxLogin", "/logout", "/login", "/actuator/**","/swagger-ui.html"
        ,"/swagger-resources","/v2/api-docs","/webjars/springfox-swagger-ui/**","/druid/**","/document/addImg"), requestURL)) {
            return true;
        }
        UserVo user = (UserVo) SecurityUtils.getSubject().getPrincipal();
        if (! SecurityUtils.getSubject().isAuthenticated()){
            // 如果没有登录, 直接返回 进入登录流程
            return  false;
        }else {
            if (antMatchers(this.toList("/role/findResourceByRoleId","/resource/findNavTree","/user/findPermissions"
            ,"/role/findAll","/user/addImg"), requestURL)){
                return true;
            }
        }
        for (Role role : user.getRoleList()) {
            //添加角色
            RoleVo roleVo = roleService.selectRoleVoByRoleId(role.getId());
            for (Resource resource : roleVo.getResourceList()) {
                if(requestURL.equals(resource.getUrl())){
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest servletRequest, ServletResponse servletResponse) throws Exception {
        HttpServletRequest request =(HttpServletRequest) servletRequest;
        HttpServletResponse response =(HttpServletResponse) servletResponse;
        WebUtils.toHttp(response).sendError(401);
        return false;
    }

    private List<String> toList(String... antPatterns) {
        List<String> antPatternsList = new ArrayList<>();
        for (String ant : antPatterns) {
            antPatternsList.add(ant);
        }
        return antPatternsList;
    }

    private boolean antMatchers(List<String> list, String uri) {
        PathMatcher matcher = new AntPathMatcher();
        for (String l : list) {
            if (matcher.match(l, uri)) {
                return true;
            }
        }
        return false;
    }
}
